package com.zz.tijian.controller;


import com.alibaba.dashscope.aigc.generation.Generation;
import com.alibaba.dashscope.aigc.generation.GenerationResult;
import com.alibaba.dashscope.aigc.generation.models.QwenParam;
import com.alibaba.dashscope.common.Message;
import com.alibaba.dashscope.exception.ApiException;
import com.alibaba.dashscope.exception.InputRequiredException;
import com.alibaba.dashscope.exception.NoApiKeyException;
import com.zz.tijian.entity.Topic;
import com.zz.tijian.entity.req.AiDto;
import com.zz.tijian.entity.req.CurrentUserInfo;
import com.zz.tijian.entity.req.TopicDto;
import com.zz.tijian.entity.res.Result;
import com.zz.tijian.utils.UserContext;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

@Api(tags = "ai咨询")
@RestController
@RequestMapping("/AIChat")
@CrossOrigin
public class AIChatController {

    @ApiOperation("调用大模型进行问答")
    @PostMapping("/consult")
    public Result<String> consult(@RequestBody AiDto aiDto){
        Message message = this.test(aiDto.getMessage(), UserContext.currentUserId());
        message.getContent();
        return Result.ok( message.getContent());
    }

    @ApiOperation("调用大模型进行问答")
    @PostMapping("/endSession")
    public Result<Boolean> endSession(){
        record.remove(UserContext.currentUserId());
        return Result.ok( true);
    }
    //记录用户历史记录
    public static Map<String , LinkedList<String>> record=new HashMap<>();

    public Message test(String question,  String token){
        //token userId
        try {
            LinkedList<String> beforList=record.get(token);
            if(beforList!=null){
                record.get(token).add(question);
            }else {
                beforList = new LinkedList<>();
                //beforList.add(question);
                record.put(token,beforList);
            }
            StringBuilder stb=new StringBuilder();
            for(String s:beforList){
                stb.append(beforList);
            }
            if(beforList.size()>=6){
                beforList.clear();
            }

            System.out.println(stb);
            Message message = callWithMessage(question,new String(stb)).getOutput().getChoices().get(0).getMessage();
            //添加历史记录
            beforList.add(new String(question+":"+message.getContent()));
            System.out.println(message);
            return message;
        } catch (ApiException | NoApiKeyException | InputRequiredException e) {
            System.out.println(e.getMessage());
        }
        return null;

    }

    public static GenerationResult callWithMessage(String inputtext, String beforeText) throws NoApiKeyException, ApiException, InputRequiredException {
        Generation gen = new Generation();

        QwenParam params = QwenParam.builder().model("qwen-turbo")
                .prompt("你是一个医生，提供的都是有关关于用户体检相关的信息，并基于这些历史问答:"+beforeText+"来回答下面这个问题:"+inputtext+",要求字数不要超过100")
                .seed(1234)
                .topP(0.8)
                .resultFormat("message")
                .enableSearch(false)
                .maxTokens(100)
                .temperature((float)0.85)
                .repetitionPenalty((float)1.0)
                .apiKey("sk-2de5aae24a4545198954dea91ca5f993")
                .build();

        GenerationResult result = gen.call(params);
        //System.out.println(result);
        return  result;
    }


}
